package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import dao.DeptInfo_Repository;
import play.data.FormFactory;
import play.libs.Json;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;
import java.util.concurrent.CompletionStage;


/**
 * The controller keeps all database operations behind the repository, and uses
 * {@link play.libs.concurrent.HttpExecutionContext} to provide access to the
 * {@link play.mvc.Http.Context} methods like {@code request()} and {@code flash()}.
 */
public class DeptInfoController extends Controller {

    private final FormFactory formFactory;
    private final DeptInfo_Repository tDeptInfoRepository;
    private final HttpExecutionContext ec;

    @Inject
    public DeptInfoController(FormFactory formFactory, DeptInfo_Repository tDeptInfoRepository, HttpExecutionContext ec) {
        this.formFactory = formFactory;
        this.tDeptInfoRepository = tDeptInfoRepository;
        this.ec = ec;
    }
    
    public CompletionStage<Result> getDeptByID() {
		JsonNode json = request().body().asJson();
		Integer DeptID = json.findPath("DeptID").intValue();
	    return tDeptInfoRepository.getDeptByID(DeptID).thenApplyAsync(_tDeptInfoEntity -> {
	    	return ok(Json.toJson(_tDeptInfoEntity));
	    }, ec.current());
    }
    
    public CompletionStage<Result> getDeptList() {
	    return tDeptInfoRepository.getDeptList().thenApplyAsync(_tDeptInfoEntity -> {
	    	return ok(Json.toJson(_tDeptInfoEntity));
	    }, ec.current());
    }

    public CompletionStage<Result> getDeptByType() {
        JsonNode json = request().body().asJson();
        Integer DeptType = json.findPath("DeptType").intValue();

        return tDeptInfoRepository.getDeptByType(DeptType).thenApplyAsync(_tDeptInfoEntity -> {
            return ok(Json.toJson(_tDeptInfoEntity));
        }, ec.current());
    }
}
